﻿/*************************************************************************
 * 
 * Filename :news
 * 
 * steven hu   2010/3/10 16:12:12
 *  
 * http://www.cnblogs.com/huxj
 *   
 * 
 * Change History:
 * 
 * 
**************************************************************************/



using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Hxj.Common;
using Hxj.Web;
using Hxj.Data;
using RicciWebsite.Model;

namespace RicciWebsite.admin.news
{
    public partial class news : RicciWebsite.Pages.AdminPage
    {
        /// <summary>
        /// 新闻ID
        /// </summary>
        protected Guid? nid;

        protected void Page_Load(object sender, EventArgs e)
        {
            string tempnid = Request.QueryString["nid"];
            if (Validator.IsGuid(tempnid))
            {
                nid = new Guid(tempnid);
            }

            if (!IsPostBack)
            {
                if (nid.HasValue)
                {
                    //修改
                    getNews();
                }
                else
                {
                    //添加
                    txtsubtime.Text = DateTime.Now.ToString("yyyy-MM-dd");
                }
            }

        }

        /// <summary>
        /// 获取新闻
        /// </summary>
        void getNews()
        {
            data_news newsModel = RicciDatabase.Default.From<data_news>().Where(data_news._.nid == nid.Value.ToString()).ToFirst();
            if (null == newsModel)
            {
                JavaScript.MsgBox_Redirect_End("该新闻不存在!", "newslist.aspx");
            }
            else
            {
                txtntitle.Text = newsModel.ntitle;
                txtcomefrom.Text = newsModel.comefrom;
                txtsubtime.Text = newsModel.subtime.ToString("yyyy-MM-dd");
                txtstatus.Value = newsModel.status.ToString();
                fckcontent.Value = newsModel.ncontent;
            }
        }


        /// <summary>
        /// 提交
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnsubmit_Click(object sender, EventArgs e)
        {
            if (!IsValid)
                return;


            if (nid.HasValue)
            {
                //修改
                Dictionary<Field, object> newvalues = new Dictionary<Field, object>();
                newvalues.Add(data_news._.comefrom, txtcomefrom.Text.Trim());
                newvalues.Add(data_news._.ncontent, fckcontent.Value);
                newvalues.Add(data_news._.ntitle, txtntitle.Text.Trim());
                newvalues.Add(data_news._.subtime, txtsubtime.Text.Trim());
                newvalues.Add(data_news._.status, txtstatus.Value);


                RicciDatabase.Default.Update<data_news>(newvalues, data_news._.nid == nid.Value.ToString());

                JavaScript.MsgBox("修改成功!");

            }
            else
            {
                //添加
                data_news newsModel = new data_news();
                Hxj.Data.Common.EntityUtils.UpdateModel<data_news>(newsModel, "txt");
                newsModel.hits = 0;
                newsModel.ncontent = fckcontent.Value;
                newsModel.nid = Guid.NewGuid().ToString();
                RicciDatabase.Default.Insert<data_news>(newsModel);

                JavaScript.MsgBox_Redirect_End("添加成功!", "newslist.aspx");

            }
        }
    }
}
